var app = getApp()
Page({
  data: {
		isUnfold:true,
    name: '',
    phone: '',
    wechatnum: ''
  },
  onLoad: function (e) {
    console.log("onload，请求内容", e);
    var that = this;
    let mid = 0;
    if (e.q) {
      mid = (e && e.q) ? decodeURIComponent(e.q).replace(app.globalData.domin + '/?', '') : 0;
    } else {
      if (e && e.mid) { 
        mid = "mid=" + e.mid;
      } else {
        if (e && (e.scene || e.query)) {
          let strLoad = e.scene ? e.scene : e.query.scene
          mid = decodeURIComponent(strLoad);
        }
      }
    }
    if (!app.globalData.plumSession) {
      console.log("登录时传递的MID:", mid);
      app.wechatSq(that, e, { parameter: mid});
    }else{
      that.requestPartners(mid);
    }
  },
  onReady: function () {
  },
  onShow: function (e) {
    app.setNavtitle('成为分销商');
  },
  requestPartners: function (scence) {
    var that = this;
    var data = {};
    data.map = 'applet_three_configure_new';
    data.mid = scence;  

    // 设置公共数据、在请求分销商申请时带上
    that.setData({
      mid: scence
    });

    console.log("===请求数据===", data);

    //发起请求，获取列表列表
    wx.showToast({
      title: '加载中',
      icon: 'loading',
      mask: true,
      duration: 10000
    });
    wx.request({
      url: app.globalData.requestUrl,
      data: data,
      success: function (res) {
        console.log("===请求分销商配置打印===",res.data);
        if (res.data.ec == 200) {
          that.setData({
            partners: res.data.data,
            isapply: res.data.data.isapply
          })
        } else {
          app.errorTip(that, res.data.em, 2000);
        }
      },
      complete: function () {
        wx.hideToast();
        wx.stopPullDownRefresh();
      }
    });
  },
  nameChange:function(e){
    var that = this;
    that.setData({
      name:e.detail.value
    })
  },
  phoneChange: function (e) {
    var that = this;
    that.setData({
      phone: e.detail.value
    })
  },
  wechatnumChange: function (e) {
    var that = this;
    that.setData({
      wechatnum: e.detail.value
    })
  },
  submitRequest: function () {
    var that = this;
    app.getSubId(that, 'applet_three_apply_distribution_new', 'toSubmitRequest');
  },
  toSubmitRequest:function(e){
    var that = this;
    var data = {
      map:'applet_three_apply_distribution_new',
      realname: that.data.name,
      mobile: that.data.phone,
      wxno: that.data.wechatnum,
      mid: that.data.mid // 带上分箱人id
    }
    var hasname = that.data.partners.hasname;
    var hasphone = that.data.partners.hasphone;
    var haswx = that.data.partners.haswx;
    if (hasname==1){
      if (!data.realname) {
        app.errorTip(that, "请输入您的姓名", 2000);
        return;
      }
    }
    if (hasphone == 1) {
      var isMob = /^1[3456789]\d{9}$/;
      if (!isMob.test(data.mobile)) {
        app.errorTip(that, "请输入正确的手机号", 2000);
        return;
      }
    }
    if (haswx == 1) {
      if (!data.wxno) {
        app.errorTip(that, "请输入您的微信号", 2000);
        return;
      }
    }
    let content = '您填写的手机号是' + data.mobile + '请确认？';
    wx.showModal({
      title: '提示',
      content: content,
      success: function (res) {
        if (res.confirm) {
          wx.showToast({
            title: '加载中',
            icon: 'loading',
            mask: true,
            duration: 10000
          });
          console.log("===分销商申请数据===", data);
          wx.request({
            url: app.globalData.requestUrl,
            data: data,
            success: function (res) {
              console.log(res.data);
              if (res.data.ec == 200) {
                app.errorTip(that, res.data.data.msg, 2000);
                let str = res.data.data.reward_amount
                str = str.split('.')[0]
                that.setData({
                  name: '',
                  phone: '',
                  wechatnum: '',
                  isapply: res.data.data.isapply,
                  successMeg: res.data.data.msg,
                  ModalShow:res.data.data.is_alert,
                  reward_amount:str,
                })
                app.globalData.isdistrib = res.data.data.isdistrib;
                app.globalData.isapply = res.data.data.isapply;
              } else {
                app.errorTip(that, res.data.em, 2000);
              }
            },
            complete: function () {
              wx.hideToast();
              wx.stopPullDownRefresh();
            }
          });
        } else {
          console.log('用户点击取消')
        }
      }
    })
  },
  GetTtNow() {
    console.log('进入的说法聚会多岁')
    app.globalData.isapply = this.data.isapply;
    this.setData({
      ModalShow:false
    })
    wx.redirectTo({
      url:'/subpages0/distributionCenter/distributionCenter'
    })
  },
})